/*
 * Created on Sep 27, 2003
 * Local Projects Database
 * Copyright (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.struts.action;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Organization;
import ro.gateway.aida.obj.db.OrganizationDB;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.BugTracker;
import ro.gateway.aida.utils.LocalisedSortableList;
import ro.gateway.aida.utils.PageToken;
import ro.gateway.aida.utils.Utils;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;


/**
 * @author Mihai Postelnicu
 * ViewOrgAction
 * This class handles the org view (list,widget,detail)
 */
public class ViewOrgAction extends TilesAction {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
        try {
            ServletContext sc = getServlet().getServletContext();
            PersistenceToken token = PersistenceManager.tokenLookup(sc);
            LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request,
                    sc);
            String lang = lang_bean.language;
            OrganizationDB orgdb = OrganizationDB.getManager(token);
            String mode = request.getParameter("mode");
            User user = UserUtils.getLogged(request, response);

            if (user == null) {
                return mapping.findForward("plslogin");
            }

            if ("view".equals(mode)) {
                String id = request.getParameter("id");

                if (id == null) {
                    return mapping.findForward("error");
                }
            }

			HttpSession session=request.getSession();
            LocalisedSortableList sl = (LocalisedSortableList) session.getAttribute(LIST);
            if (sl==null || "widget".equals(mode)) 
            {
            	sl=orgdb.getList(user.getId());
            	session.setAttribute(LIST,sl);
        	} 
        	if (sl.getViewLang()!=lang) {
				sl.setViewLang(lang);
				sl.setComparator(new Organization.LangComparator(lang));
        	}
        	PageToken.tokenizeList( request,  sc, sl, "orgs");
           

            if ("widget".equals(mode)) {
                return mapping.findForward("orgwidget");
            }

            Utils.setBreadCrumbs(request, "bc.org.list");

            return mapping.findForward("orglist");
        } catch (Exception e) {
            BugTracker.publishBug(e, request);

            return mapping.findForward("fail");
        }
    }
    public static final String LIST="ViewOrgAction_LIST";
}
